﻿#pragma warning disable 1591

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace TotalFreedomBST
{
    public class CarouselSection : BstSection
    {
        private HtmlHelper _htmlHelper;

        public Carousel Carousel { get; set; }

        public CarouselSection(HtmlHelper htmlHelper, string id = null, Carousel carousel = null)
            : base(htmlHelper.ViewContext, "</div>\r\n")
        {
            if (carousel != null)
                Carousel = carousel;
            else
                Carousel = new Carousel(_htmlHelper, id);


            _htmlHelper = htmlHelper;
        }

        protected override string EndHtml()
        {
            string s = "</div>\r\n<a class=\"left carousel-control\" href=\"#" + Carousel.Id + "\" data-slide=\"prev\">&lsaquo;</a>\r\n";
            s += "<a class=\"right carousel-control\" href=\"#" + Carousel.Id + "\" data-slide=\"next\">&rsaquo;</a>";

            return s;
        }

        public override string StartHtml()
        {
            string s = "<div class=\"carousel slide\"";
            if(Carousel.Interval > 0)
                s += "data-interval=\"" + Carousel.Interval + "\"";
            else
                s += "data-interval=\"false\"";

            s += " id=\"" + Carousel.Id + "\">\r\n";

            s += "<ol class=\"carousel-indicators\">\r\n";
            foreach (CarouselItem carouselItem in Carousel.CarouselItems)
            {
                s += "<li data-target=\"#" + Carousel.Id + "\" data-slide-to=\"" + carouselItem.ItemNumber + "\" ";
                if (((carouselItem.Options & CarouselItemOptions.Active) == CarouselItemOptions.Active))
                    s += " class=\"active\"";

                s += "></li>\r\n";
            }
            s += "</ol>\r\n";

            s += "<div class=\"carousel-inner\">\r\n";

            return s;
        }

        public CarouselItemSection CarouselItem(CarouselItem carouselItem, object htmlAttributes = null)
        {
            CarouselItemSection carouselItemSection = new CarouselItemSection(this.Carousel, _htmlHelper.ViewContext, carouselItem);

            _htmlHelper.ViewContext.Writer.Write(carouselItemSection.StartHtml());

            return carouselItemSection;
        }

        public CarouselItem CreateCarouselItem(string id = null)
        {
            return Carousel.CreateCarouselItem(id);
        }        
    }    

    public class Carousel 
    {
        public HtmlHelper HtmlHelper { get; private set; }
        public string Id { get; set; }

        /// <summary>
        /// Interval of a cycle in 
        /// </summary>
        public int Interval { get; set; }

        public readonly List<CarouselItem> CarouselItems = new List<CarouselItem>();

        public Carousel(HtmlHelper htmlHelper, string id)
        {
            Interval = 5000;
            HtmlHelper = htmlHelper;
            if (String.IsNullOrWhiteSpace(id))
                Id = System.Guid.NewGuid().ToString("N");
            else
                Id = id;
        }

        public CarouselItem CreateCarouselItem(string caption, CarouselItemOptions options = CarouselItemOptions.None, string id = null)
        {
            return CreateCarouselItem(new MvcHtmlString(HttpUtility.HtmlEncode(caption)), options, id);
        }

        public CarouselItem CreateCarouselItem(IHtmlString caption, CarouselItemOptions options =  CarouselItemOptions.None, string id = null)
        {
            CarouselItem carouselItem = new CarouselItem(this, caption, options, id);
            carouselItem.ItemNumber = CarouselItems.Count;
            CarouselItems.Add(carouselItem);

            return carouselItem;
        }

        public MvcHtmlString ToPrev()
        {
            string s = "$('#" + Id + "').carousel('prev');";
            return new MvcHtmlString(s);
        }

        public MvcHtmlString ToNext()
        {
            string s = "$('#" + Id + "').carousel('next');";
            return new MvcHtmlString(s);
        }

        public MvcHtmlString Cycle()
        {
            string s = "$('#" + Id + "').carousel('cycle');";
            return new MvcHtmlString(s);
        }

        public MvcHtmlString Pause()
        {
            string s = "$('#" + Id + "').carousel('pause');";
            return new MvcHtmlString(s);
        }
    }

    public class CarouselItemSection : BstSection
    {
        private Carousel _carousel;
        public CarouselItem CarouselItem { get; set; }

        public CarouselItemSection(Carousel carousel, ViewContext viewContext, CarouselItem carouselItem, string id = null)
            : base(viewContext, "</div>\r\n")
        {
            if (carousel == null)
                throw (new ArgumentNullException());

            _carousel = carousel;
            CarouselItem = carouselItem;
        }

        protected override string EndHtml()
        {
            string s = "<div class=\"carousel-caption\">";
            s += CarouselItem.Caption.ToHtmlString();
            s += "</div>\r\n";

            return s;
        }

        public override string StartHtml()
        {
            string s = "<div class=\"item";

            bool isActive = (CarouselItem.Options & CarouselItemOptions.Active) == CarouselItemOptions.Active;
            if (isActive)
                s += " active";

            s += "\" id=\"" + CarouselItem.Id + "\">\r\n";
            return s;
        }
    }

    [FlagsAttribute]
    public enum CarouselItemOptions
    {
        None = 0,
        Active = 1,
    }

    public class CarouselItem
    {
        private readonly Carousel _carousel;
        public string Id { get; set; }
        public int ItemNumber { get; set; }
        public IHtmlString Caption { get; set; }
        public CarouselItemOptions Options { get; set; }
        public CarouselItem ParentCarouselItem { get; set; }

        public CarouselItem(Carousel carousel, IHtmlString caption, CarouselItemOptions options, string id = null)
        {
            Caption = caption;
            Options = options;
            _carousel = carousel;

            if (String.IsNullOrWhiteSpace(id))
                Id = System.Guid.NewGuid().ToString("N");
            else
                Id = id;
        }

        public MvcHtmlString Activate()
        {
            string s = "$('#" + _carousel.Id + "').carousel(" + ItemNumber + ");";
            return new MvcHtmlString(s);
        }
        
        public MvcHtmlString FromPartial(string partialName, object model = null, ViewDataDictionary viewData = null)
        {
            string s = "<div class=\"item";

            if ((Options & CarouselItemOptions.Active) == CarouselItemOptions.Active)
                s += " active";

            s += "\" id=\"" + Id + "\">\r\n";
            s += _carousel.HtmlHelper.Partial(partialName, model, viewData).ToHtmlString();
            s += "\r\n<div class=\"carousel-caption\">\r\n";
            s += Caption.ToHtmlString();
            s += "</div>\r\n";
            s += "</div>\r\n";

            return new MvcHtmlString(s);
        }        
    }
}
